﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class BaiTraLoiDAO
    {
        public static List<BaiTraLoiDTO> getAll()
        {
            List<BaiTraLoiDTO> result = new List<BaiTraLoiDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.BAITRALOIs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                BaiTraLoiDTO dto = new BaiTraLoiDTO();

                dto.MaBaiTraLoi = ds.MaBaiTraLoi;
                dto.MaTinRaoVat = ds.MaTinRaoVat.Value;
                dto.TraLoiCho = ds.TraLoiCho.Value;
                dto.NoiDungTraLoi = ds.NoiDungTraLoi;
                dto.ThoiGianTraLoi = ds.ThoiGianTraLoi.Value;
                dto.MaNguoiDung = ds.MaNguoiDung.Value;

                result.Add(dto);             
            }

            return result;
        }

        public static BaiTraLoiDTO getAlongCode(int maBaiTraLoi)
        {
            BaiTraLoiDTO result = new BaiTraLoiDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.BAITRALOIs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaBaiTraLoi == maBaiTraLoi).Single();

            result.MaBaiTraLoi = query.MaBaiTraLoi;
            result.MaTinRaoVat = query.MaTinRaoVat.Value;
            result.TraLoiCho = query.TraLoiCho.Value;
            result.NoiDungTraLoi = query.NoiDungTraLoi;
            result.ThoiGianTraLoi = query.ThoiGianTraLoi.Value;
            result.MaNguoiDung = query.MaNguoiDung.Value;                            

            return result;
        }

        public static void Insert(BaiTraLoiDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            BAITRALOI baitraloi = new BAITRALOI();
            baitraloi.TraLoiCho = _insert.TraLoiCho;
            baitraloi.NoiDungTraLoi = _insert.NoiDungTraLoi;
            baitraloi.ThoiGianTraLoi = _insert.ThoiGianTraLoi;
            baitraloi.Deleted = _insert.Deleted;
            baitraloi.MaNguoiDung = _insert.MaNguoiDung;
            baitraloi.MaTinRaoVat = _insert.MaTinRaoVat;

            dbRaoVat.BAITRALOIs.InsertOnSubmit(baitraloi);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maBaiTraLoi)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            BAITRALOI _delete = (from n in dbRaoVat.BAITRALOIs
                          where n.MaBaiTraLoi == maBaiTraLoi && n.Deleted == 0
                          select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();            
        }

        public static void Update(BaiTraLoiDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            BAITRALOI _update = (from n in dbRaoVat.BAITRALOIs
                                   where n.MaBaiTraLoi == dto.MaBaiTraLoi && n.Deleted == 0
                                   select n).Single();

            _update.MaTinRaoVat = dto.MaTinRaoVat;
            _update.TraLoiCho = dto.TraLoiCho;
            _update.NoiDungTraLoi = dto.NoiDungTraLoi;
            _update.ThoiGianTraLoi = dto.ThoiGianTraLoi;
            _update.MaNguoiDung = dto.MaNguoiDung;            

            dbRaoVat.SubmitChanges();
        }
    }
}
